import { createSlice } from '@reduxjs/toolkit';
 
// 定义函数来计算 initialState
// const calculateInitialState = (loopCount: any) => {
//   let initialState = [];
//   for (let i = 0; i < loopCount; i++) {
//     initialState.push(i);
//   }
//   return initialState;
// };
 
const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    count: 10,
    // data: calculateInitialState(5)
  },
  reducers: {
    //一个方法(递增)根据自身需求更改
    increment: (state) => {
      state.count += 1;
    },
    //一个方法(减 payload)根据自身需求更改
    decrement: (state, action) => {
      const { payload } = action;
      state.count -= payload;
    },
  },
});
//从 counterSlice.actions 中解构出 increment 和 decrement action creators，以便在应用程序中使用它们来分发相应的 action
export const { increment, decrement } = counterSlice.actions;
//返回 reducer供combineReducers组装
export default counterSlice.reducer;